package com.example.springboot.service.Impl;

import cn.hutool.core.collection.CollUtil;
import com.example.springboot.controller.request.BaseRequest;
import com.example.springboot.entity.Book;
import com.example.springboot.mapper.BookMapper;
import com.example.springboot.service.BookService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.List;

@Service
@Slf4j
public class BookServiceImpl implements BookService {


    @Resource
    private BookMapper bookMapper;

    @Override
    public List<Book> list() {
        return bookMapper.list();
    }

    @Override
    public PageInfo<Book> page(BaseRequest baseRequest) {
        PageHelper.startPage(baseRequest.getPageNum(),baseRequest.getPageSize());
        List<Book> books = bookMapper.listByCondition(baseRequest);
        PageInfo<Book> BookPageInfo = new PageInfo<>(books);
        return BookPageInfo;
    }

    @Override
    public void save(Book obj) {
        obj.setCategory(category(obj.getCategories()));
        bookMapper.save(obj);
    }

    @Override
    public Book getById(Integer id) {
        Book Book = bookMapper.getById(id);
        return Book;
    }

    @Override
    public void update(Book obj) {
        obj.setCategory(category(obj.getCategories()));
        obj.setUpdatetime(LocalDate.now());
        bookMapper.updateById(obj);
    }

    @Override
    public void deleteById(Integer id) {
        bookMapper.deleteById(id);
    }


    private String category(List<String> categories){
        StringBuilder sb = new StringBuilder();
        if (CollUtil.isNotEmpty(categories)){
            categories.forEach(v -> sb.append(v).append(">"));
            return sb.substring(0,sb.lastIndexOf(">"));
        }
        return sb.toString();
    }
}
